// pages/registerbox/register1/register1.js
import {
  getRegisterInit
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataInfo: null,
    teamnum: "",
    ordernum: "",
    leibie: "",
    name: "",
    phone: "",
    sendaddress: "",
    cindex: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    getRegisterInit((r) => {
      if (r.code == 0) {
        self.setData({
          dataInfo: r.data
        })
      } else {
        errorMsg(r.message)
      }
    })
  },
  //获取类别
  getLeiBie: function(e) {
    this.setData({
      leibie: e.detail.value
    })
  },
  //获取发件人名字
  getName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  //获取发件人电话
  getPhone: function(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //获取发件人地址
  getsendAddress: function(e) {
    this.setData({
      sendaddress: e.detail.value
    })
  },
  //所属分类
  bindPickerChange0: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      cindex: e.detail.value
    })
  },
  //每日订单量
  bindPickerChange1: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      ordernum: e.detail.value
    })
  },
  //目前团队人数
  bindPickerChange2: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      teamnum: e.detail.value
    })
  },
  //下一步
  goNext: function() {
    let {
      leibie,
      name,
      phone,
      ordernum,
      teamnum,
      dataInfo,
      cindex,
      sendaddress
    } = this.data;

    if (cindex == "") {
      errorMsg("请填写商品类别")
    } else if (ordernum == "") {
      errorMsg("请选择每日订单量")
    } else if (teamnum == "") {
      errorMsg("请选择团队人数")
    } else if (name == "") {
      errorMsg("请输入发件人姓名")
    } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(phone)) {
      errorMsg("发件人手机号不正确")
    } else { 
      let registerInfo = {
        leibie: dataInfo.category[cindex].name,
        name,
        phone,
        sendaddress,
        ordernum: ordernum !== "" ? dataInfo.order_per_day[ordernum].id : "",
        teamnum: teamnum !== "" ? dataInfo.team_members[teamnum].id : "",
      }
      wx.navigateTo({
        url: '../register2/register2?registerInfo=' + JSON.stringify(registerInfo),
      })
    }

  }

})